import pymysql
import datetime
import requests
'开播的直播间数（功能4,5,6）'

# 各分区的接口
urlArr = [
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100022&tagAll=0&page=",
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100004&tagAll=0&page=",
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100023&tagAll=0&page=",
    "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=100002&tagAll=0&page="
]
# 各分区名
areaNameArr = ["娱乐天地", "手游休闲", "网游竞技", "单机热游"]


# 接口爬虫
def getInterData(url):
    # 页码
    page = 1
    # 爬取的数据
    result = []
    # 爬取9页够了，不需要太多
    while True:
        # 获取接口返回数据
        res = requests.get(url + str(page))
        # 转成json格式
        response = res.json()
        # 获取详细内容
        artists = response['data']['datas']
        # 如果返回的为空，就说明爬取完了，退出函数
        if len(artists) == 0:
            break
        result.extend(artists)
        print("第%s页" % page)
        page += 1
        # 防止ip被禁用，限制5秒爬一次
        # time.sleep(5)
    return result


# 计算一下每个分区的开播数
def computeLiveCount(artists):
    return len(artists)


# 创建MySQL连接函数（当做固定）
def getMysqlConn():
    conn = pymysql.connect(host='106.13.194.7',
                           port=3306,
                           user='rdsroot',
                           password='abc123456',
                           db='huya',
                           charset='utf8')
    # 创建操作的游标
    cursor = conn.cursor()
    # 设置字符编码及自动提交
    cursor.execute('set names utf8')  # 固定格式
    cursor.execute('set autocommit=1')
    # 设置自动提交
    return conn, cursor


# 保存分区数据
def saveAreaData(areaName, liveCount):
    conn, cursor = getMysqlConn()
    # sql语句
    sql = "insert into t_liveCount(id,areaName,liveCount,date) values(REPLACE(UUID(),'-',''),%s,%s,%s)"

    add_data = (areaName, liveCount,
                datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    # 这里会把sql语句中的%s替换掉
    cursor.execute(sql, add_data)
    print("mysql插入数据完成")
    # 关闭连接
    cursor.close()
    conn.close()


# 定时任务调用这个函数
def requestLiveCount():
    i = 0
    while i < 4:
        res = getInterData(urlArr[i])
        count = computeLiveCount(res)
        saveAreaData(areaNameArr[i], count)
        i = i + 1


# 单元测试用
if __name__ == '__main__':
    requestLiveCount()
